// include the basic windows header files and the Direct3D header file
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>

// include the Direct3D Library file
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

#include "GlobalUse.h"
#include "BaseD3D.h"
#include "Input.h"

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow) {
    HWND hWnd;											//Window class
    WNDCLASSEX wc;										//Window Information

    ZeroMemory(&wc, sizeof(WNDCLASSEX));				//Empty Memory

    wc.cbSize = sizeof(WNDCLASSEX);						//Init size of wininfo
    wc.style = CS_HREDRAW | CS_VREDRAW;					//Redraw on both dimension
    wc.lpfnWndProc = (WNDPROC)WindowProc;				//???
    wc.hInstance = hInstance;							//
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);			//Cursor
    wc.lpszClassName = _T("WindowClass1");				//Class name... or window name?

    RegisterClassEx(&wc);								//Register this class

    hWnd = CreateWindowEx(NULL,							//Create window
                          _T("WindowClass1"),			//Class name(again?)
                          _T("Our Direct3D Program"),	//Window name
                          //WS_EX_TOPMOST | WS_POPUP,	//Full screen
                          WS_OVERLAPPEDWINDOW,			//Window screen
						  80, 60,						//Start window position
                          SCREEN_WIDTH, SCREEN_HEIGHT,	//Screen width and heigth
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hWnd, nCmdShow);							//Make the window appear
	gfSetMainWin(hWnd);

	VBaseD3D* baseD3D;									//Base D3D class
	baseD3D = new VBaseD3D();						//Start new base instance
	gfSetD3DBase(baseD3D);
	baseD3D = gfGetD3DBase();
    // set up and initialize Direct3D
    baseD3D->InitD3D();								//Initialize base

	VInput* inputter;
	inputter = new VInput();
	gfSetInputter(inputter);
	inputter = gfGetInputter();
	inputter->InitInput();

    // enter the main loop:

    MSG msg;											//Message bar

	while(TRUE)	{										//Loop forever
        DWORD starting_point = GetTickCount();			//Get the tick count

		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {	//Look at the received message
		//if (GetMessage(&msg,NULL,0,0)) {
			if (msg.message == WM_QUIT) {				//If message tell you to quit
                break;									//Then quit
			}
			else if (msg.message == WM_INPUT) {
				VInput* input = gfGetInputter();
				input->UpdateInputInfo(msg.lParam);
			}

            TranslateMessage(&msg);						//Translate
            DispatchMessage(&msg);						//Dispatch
        }

        baseD3D->RenderFrame();							//Render image

		if(KEY_DOWN(VK_ESCAPE)) {						//Check the 'escape' key
            PostMessage(hWnd, WM_DESTROY, 0, 0);		//Destroy window if pressed
		}

        while ((GetTickCount() - starting_point) < 25);
    }

    //Clean up DirectX and COM
    delete baseD3D;
	baseD3D = NULL;

    return msg.wParam;
}


// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch(message) {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } 
			break;
    }

    return DefWindowProc (hWnd, message, wParam, lParam);
}

